# Scheme 48 Makefile
# Documentation in files INSTALL and doc/install.txt

SHELL = /bin/sh

### Filled in by `configure' ###
srcdir = @srcdir@
VPATH = @srcdir@
CC = @CC@
LD = @LD@
DEFS = @DEFS@ -D__COMPILING_SCHEME48_ITSELF__ -DS48_HOST_ARCHITECTURE=\"$(HOST_ARCHITECTURE)\"
LIBS = @LIBS@
CFLAGS = @CFLAGS@
CPPFLAGS =
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@

LDFLAGS = @LDFLAGS@
LDFLAGS_VM = @LDFLAGS_VM@
LIBOBJS = @LIBOBJS@

GC_OBJS = @GC_OBJS@

HOST_ARCHITECTURE = @host@
BIT_SUFFIX = @BIT_SUFFIX@

prefix = @prefix@
exec_prefix = @exec_prefix@

bindir = @bindir@
libdir = @libdir@
incdir = @includedir@
manext = 1
mandir = @mandir@/man$(manext)
datarootdir = @datarootdir@
datadir = @datadir@

DYNAMIC_EXTERNALS_CFLAGS=@DYNAMIC_EXTERNALS_CFLAGS@
DYNAMIC_EXTERNALS_LDFLAGS=@DYNAMIC_EXTERNALS_LDFLAGS@
DYNAMIC_EXTERNALS_LDFLAGS_IN_PLACE=@DYNAMIC_EXTERNALS_LDFLAGS_IN_PLACE@
### End of `configure' section###

# HP 9000 series, if you don't have gcc
#   CC = cc
#   CFLAGS = -Aa -O +Obb1800
#   DEFS = -D_HPUX_SOURCE -Dhpux

# Ultrix
#   LDFLAGS = -N

.c.o:
	$(CC) -c $(CPPFLAGS) $(DEFS) -I ./c -I$(srcdir)/c -I$(srcdir)/c/bibop $(CFLAGS) -o $@ $<

# You might want to change RUNNABLE to "s48"
VERSION = 1.`cat $(srcdir)/build/minor-version-number`
RUNNABLE = scheme48
CONFIG_SCRIPT = scheme48-config
MANPAGE = $(RUNNABLE).$(manext)
LIB = $(libdir)/scheme48-$(VERSION)
SHARE = $(datadir)/scheme48-$(VERSION)

distdir = /tmp

# If make barfs on this include line, just comment it out.  It's only
# really needed if you want to build the linker or rebuild initial.image.
include $(srcdir)/build/filenames.make
#
#NetBSD make wants to see this instead:
#.include "$(srcdir)/build/filenames.make"


# Static linker:
#
# You only need the linker if you're going to make changes to the
# things that go into the initial.image, which in general means the
# files in rts/.  If you decide you need to use the linker, then you
# gots your choice; it can run in just about any version of Scheme 48
# or Pseudoscheme.  (It has also been made to run in Scheme->C.)  It
# doesn't matter a whole lot which Scheme you use as long as it's not
# broken or unavailable.  The two best choices are:
#
# 1. As below.
#    These settings requires you to already have a $(RUNNABLE)
#    command.  This is desirable if you are making changes to the
#    system that might break scheme48vm and/or scheme48.image.  But it
#    requires you to have squirreled away a previous working version
#    of scheme48.  The settings assume a Unicode version; if you want
#    to use an installed non-Unicode-capable Scheme 48 (1.4 or earlier),
#    you need to use this setting:
#    LINKER_WRITEBYTE = ,load scheme/alt/write-byte.scm
#
# 2. LINKER_VM = ./$(VM) $(BIG_HEAP)
#    LINKER_RUNNABLE = $(LINKER_VM) -i $(IMAGE)
#    LINKER_WRITEBYTE = ,open (subset i/o (write-byte)) (subset ports (set-port-crlf?!))
#    This builds the linker on the scheme48vm and scheme48.image
#    that are in the current directory.

BIG_HEAP = -h 8000000
LINKER_VM = $(RUNNABLE) $(BIG_HEAP)
LINKER_RUNNABLE = $(LINKER_VM)
LINKER_WRITEBYTE = ,open (subset i/o (write-byte)) (subset ports (set-port-crlf?!))
START_LINKER-32 = echo ',batch';				\
	       echo ',bench on';				\
	       echo ',open signals handle features';		\
	       echo ',open bitwise ascii code-vectors';		\
	       echo '$(LINKER_WRITEBYTE)';			\
	       echo ',config ,load scheme/platform-interfaces.scm'; \
	       echo ',config ,load scheme/rts-packages-32.scm'; \
	       echo ',open platform';	\
	       echo ',open cells record-types';	\
	       echo ',load $(linker-files)';			\
	       echo ',load scheme/alt/init-defpackage.scm'

START_LINKER-64 = echo ',batch';				\
	       echo ',bench on';				\
	       echo ',open signals handle features';		\
	       echo ',open bitwise ascii code-vectors';		\
	       echo '$(LINKER_WRITEBYTE)';			\
	       echo ',config ,load scheme/platform-interfaces.scm'; \
	       echo ',config ,load scheme/rts-packages-64.scm'; \
	       echo ',open platform';	\
	       echo ',open cells record-types';	\
	       echo ',load $(linker-files)';			\
	       echo ',load scheme/alt/init-defpackage.scm'

# --------------------
# You shouldn't have to change anything below this point.

# Targets:

IMAGE = scheme48.image
INITIAL = $(srcdir)/build/initial.image
VM = scheme48vm
POSIX_EXTERNAL = c/posix.so
SRFI_27_EXTERNAL = c/srfi-27.so
UNIX_OBJS = c/unix/misc.o c/unix/io.o c/unix/fd-io.o c/unix/event.o
OBJS =	c/scheme48vm-$(BIT_SUFFIX).o \
	c/extension.o c/free.o c/double_to_string.o c/bignum.o \
	c/external.o c/external-lib.o c/external-init.o c/init.o

FAKEHS	= c/fake/dlfcn.h c/fake/sigact.h c/fake/strerror.h \
		c/fake/sys-select.h c/fake/langinfo.h

SCHEME48HS = c/scheme48.h c/scheme48arch.h c/scheme48write-barrier.h

# Sources:

CONFIG_FILES = scheme/interfaces.scm scheme/packages.scm \
	       scheme/vm/shared-interfaces.scm	\
	       scheme/low-packages.scm scheme/rts-packages.scm \
	       scheme/comp-packages.scm scheme/initial-packages.scm

# Rules:

# The following is the first rule and therefore the "make" command's
# default target.
enough: vm $(POSIX_EXTERNAL) $(SRFI_27_EXTERNAL) $(IMAGE) script-interpreter go

# --------------------
# External code to include in the VM
# After changing any of these you should delete `scheme48vm' and remake it.

EXTERNAL_OBJECTS = c/unix/socket.o c/unix/dynlink.o \
		   c/unix/sysexits.o $(ASM_OBJECTS)

# Rules for any external code.

# POSIX rules; this could have its own Makefile, but I don't want to bother.

posix_dir = c/posix

CC_POSIX = $(CC) -c $(CPPFLAGS) $(DEFS) -I ./c -I$(srcdir)/c -I$(srcdir)/c/bibop $(CFLAGS) $(DYNAMIC_EXTERNALS_CFLAGS) -o $@ $<

$(posix_dir)/user.o: c/posix/user.c \
	$(SCHEME48HS) c/c-mods.h $(posix_dir)/posix.h
	$(CC_POSIX)

$(posix_dir)/regexp.o: c/posix/regexp.c \
	$(SCHEME48HS) c/c-mods.h $(posix_dir)/posix.h
	$(CC_POSIX)

$(posix_dir)/proc-env.o: c/posix/proc-env.c \
	$(SCHEME48HS) c/c-mods.h $(posix_dir)/posix.h
	$(CC_POSIX)

$(posix_dir)/io.o: c/posix/io.c \
	$(SCHEME48HS) c/c-mods.h $(posix_dir)/posix.h
	$(CC_POSIX)

$(posix_dir)/proc.o: c/posix/proc.c \
	$(SCHEME48HS) c/c-mods.h \
	c/event.h $(posix_dir)/posix.h $(posix_dir)/s48_signals.h
	$(CC_POSIX)

$(posix_dir)/posix.o: c/posix/posix.c
	$(CC_POSIX)

$(posix_dir)/dir.o: c/posix/dir.c \
  $(SCHEME48HS) c/scheme48heap.h c/c-mods.h \
  c/event.h c/fd-io.h $(posix_dir)/posix.h
	$(CC_POSIX)

POSIX_OBJECTS = $(posix_dir)/user.o     $(posix_dir)/regexp.o	\
                $(posix_dir)/proc-env.o $(posix_dir)/proc.o	\
                $(posix_dir)/io.o	$(posix_dir)/dir.o	\
		$(posix_dir)/posix.o

$(POSIX_EXTERNAL) : $(POSIX_OBJECTS)
	$(LD) -o $@ $(POSIX_OBJECTS) $(DYNAMIC_EXTERNALS_LDFLAGS_IN_PLACE) 


# End of POSIX rules

c/srfi-27.o: c/srfi-27.c $(SCHEM48HS)
	$(CC) -c $(CPPFLAGS) $(DEFS) -I ./c -I$(srcdir)/c -I$(srcdir)/c/bibop $(CFLAGS) $(DYNAMIC_EXTERNALS_CFLAGS) -o $@ $<

$(SRFI_27_EXTERNAL) : c/srfi-27.o
	$(LD) -o $@ $^ $(DYNAMIC_EXTERNALS_LDFLAGS_IN_PLACE)

# Native-code glue rules

ASM_OBJECTS = c/fake/glue.o
ASM_INITIALIZERS =

# Real definitions to be used when a. native code works and b. it is supported
# on the machine we're compiling on
#ASM_OBJECTS = c/glue.o c/asm-glue.o
#ASM_INITIALIZERS =	s48_init_asm_glue

# End of native-code glue rules

# Two-space copier objects

GC_TWOSPACE_OBJS = c/scheme48-twospace-heap-$(BIT_SUFFIX).o

c/scheme48-twospace-heap-$(BIT_SUFFIX).o: c/prescheme.h c/scheme48vm.h c/scheme48heap.h \
			c/scheme48vm-prelude.h c/c-mods.h \
			c/event.h c/io.h c/fd-io.h

# The BIBOP Garbage Collector
GC_BIBOP_OBJS =  c/scheme48-bibop-heap-$(BIT_SUFFIX).o \
	c/bibop/page_alloc.o c/bibop/memory_map.o c/bibop/areas.o \
	c/bibop/generation_gc.o c/bibop/find_all.o c/bibop/check_heap.o \
	c/bibop/utils.o c/bibop/area_roots.o c/bibop/remset.o

bibop: $(GC_BIBOP_OBJS)

c/scheme48-bibop-heap-$(BIT_SUFFIX).o: c/prescheme.h c/scheme48vm.h c/scheme48heap.h \
			c/scheme48vm-prelude.h \
			c/bibop/bibop.h \
			c/c-mods.h c/event.h c/io.h c/fd-io.h

c/bibop/page_alloc.o: c/bibop/page_alloc.h c/bibop/page_constants.h            \
	c/bibop/memory.h
c/bibop/memory_map.o: c/bibop/memory_map.h c/bibop/utils.h                     \
	c/bibop/page_constants.h
c/bibop/areas.o: c/bibop/areas.h c/bibop/page_constants.h c/bibop/utils.h      \
	c/bibop/memory_map.h c/bibop/area_roots.h c/bibop/gc_config.h          \
	c/bibop/remset.h
c/bibop/generation_gc.o: c/bibop/generation_gc.h c/bibop/page_constants.h      \
	c/bibop/areas.h c/bibop/memory.h c/bibop/data.h c/bibop/utils.h        \
	c/bibop/memory_map.h c/bibop/check_heap.h c/bibop/area_roots.h         \
	c/bibop/measure.h c/bibop/measure.ci 				       \
	c/bibop/gc_config.h c/bibop/remset.h				       \
	$(SCHEME48HS)
c/bibop/find_all.o: c/bibop/find_all.h c/bibop/generation_gc.h c/bibop/areas.h \
	c/bibop/data.h c/bibop/memory.h					       \
	$(SCHEME48HS)
c/bibop/check_heap.o: c/bibop/check_heap.h c/scheme48.h c/bibop/generation_gc.h\
	c/bibop/utils.h c/bibop/data.h c/bibop/memory.h c/bibop/memory_map.h   \
	$(SCHEME48HS)
c/bibop/area_roots.o: c/bibop/area_roots.h c/bibop/areas.h c/bibop/memory.h    \
	c/bibop/memory_map.h c/bibop/measure.h c/bibop/gc_config.h             \
	c/bibop/remset.h
c/bibop/remset.o: c/bibop/remset.h c/bibop/memory.h c/scheme48.h	       \
	c/bibop/utils.h c/bibop/data.h c/bibop/generation_gc.h 		       \
	c/bibop/gc_config.h						       \
	$(SCHEME48HS)


# End of BIBOP GC rules


$(VM): c/main.o $(OBJS) $(GC_OBJS) $(UNIX_OBJS) $(LIBOBJS) $(EXTERNAL_OBJECTS) \
	c/scheme48.exp c/scheme48.def
	$(CC) $(LDFLAGS) $(LDFLAGS_VM) $(CFLAGS) -o $@ c/main.o		\
		$(OBJS) $(GC_OBJS) $(UNIX_OBJS)				\
		$(LIBOBJS) $(LIBS)					\
		$(EXTERNAL_OBJECTS)
	# Cygwin
	if test -x /usr/bin/dlltool; then \
		dlltool --dllname $(VM).exe --output-lib $(VM).a --def $(srcdir)/c/scheme48.def; \
	fi

script-interpreter: c/script-interpreter.o
	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ c/script-interpreter.o

c/main.o: c/main.c c/scheme48vm.h c/scheme48heap.h
	$(CC) -c $(CFLAGS) -o $@ \
		-DDEFAULT_IMAGE_NAME=\"$(LIB)/$(IMAGE)\" \
		$(CPPFLAGS) $(DEFS) $(srcdir)/c/main.c

c/scheme48vm-$(BIT_SUFFIX).o: c/prescheme.h c/scheme48vm.h \
		c/scheme48heap.h \
		c/scheme48image.h c/bignum.h c/event.h \
		c/io.h c/fd-io.h \
		c/scheme48vm-prelude.h c/c-mods.h \
		$(SCHEME48HS)

c/bignum.o: c/bignum.h c/bignumint.h $(SCHEME48HS)
c/extension.o: c/sysdep.h $(FAKEHS) $(SCHEME48HS) c/scheme48vm.h
c/free.o: c/sysdep.h
c/external.o: c/sysdep.h $(FAKEHS) $(SCHEME48HS) c/c-mods.h
c/unix/event.o: c/sysdep.h $(FAKEHS) c/scheme48vm.h c/scheme48heap.h \
			c/event.h c/fd-io.h
c/unix/fd-io.o: c/sysdep.h $(FAKEHS) c/scheme48vm.h c/scheme48heap.h \
			c/event.h c/fd-io.h
c/unix/misc.o: c/sysdep.h $(FAKEHS)
c/unix/io.o: c/io.h

c/unix/socket.o: $(SCHEME48HS) c/fd-io.h c/event.h
c/unix/dynlink.o: $(SCHEME48HS)

c/script-interpreter.o: c/sysdep.h $(FAKEHS) c/script-interpreter.c
	$(CC) -c $(CFLAGS) -o $@ \
		-DRUNNABLE=\"$(bindir)/$(RUNNABLE)\" \
		$(CPPFLAGS) $(DEFS) \
		-I ./c -I$(srcdir)/c \
		$(srcdir)/c/script-interpreter.c
c/fake/strerror.o: c/fake/strerror.h
c/fake/langinfo.o: c/fake/langinfo.h

# --------------------
# Make scheme48.image from initial.image and library .scm files.
#
# For bootstrap reasons, initial.image is *not* listed as a source,
# even though it really is.

$(IMAGE): $(VM) scheme/env/init-defpackage.scm scheme/more-interfaces.scm \
		scheme/link-packages.scm scheme/env-packages.scm \
		scheme/more-packages.scm \
		scheme/sort/interfaces.scm scheme/sort/packages.scm \
		scheme/cml/interfaces.scm scheme/cml/packages.scm \
		scheme/posix/packages.scm scheme/srfi/packages.scm \
		$(usual-files) build/initial.debug build/build-usual-image
	$(srcdir)/build/build-usual-image $(srcdir) \
		"`(cd $(srcdir) && pwd)`/scheme" "`pwd`/c" '$(IMAGE)' './$(VM)' \
		'$(INITIAL)-$(BIT_SUFFIX)'

### Fake targets:  all clean install man dist

install: enough dirs \
	 inst-script inst-vm inst-misc inst-man inst-inc inst-image \
	 inst-script-interpreters inst-config-script inst-externals

inst-vm:
	$(INSTALL_PROGRAM) $(VM) $(DESTDIR)$(LIB)
	if [-e $(VM).a]; then $(INSTALL_PROGRAM) $(VM).a $(DESTDIR)$(LIB); fi

inst-image:
	rm -f '/tmp/$(IMAGE)'
	$(srcdir)/build/build-usual-image $(srcdir) "$(SHARE)" "$(LIB)" \
		"/tmp/$(IMAGE)" "./$(VM)" "$(INITIAL)-$(BIT_SUFFIX)"
	$(INSTALL_DATA) /tmp/$(IMAGE) $(DESTDIR)$(LIB)
	rm /tmp/$(IMAGE)

inst-man:
	$(srcdir)/mkinstalldirs $(DESTDIR)$(mandir)
	sed 's=LBIN=$(bindir)=g' $(srcdir)/doc/scheme48.man |	   \
		sed 's=LLIB=$(LIB)=g' |				   \
		sed 's=LS48=$(RUNNABLE)=g' >$(MANPAGE)
	$(INSTALL_DATA) $(MANPAGE) $(DESTDIR)$(mandir)
	rm $(MANPAGE)

inst-inc: c/scheme48.exp c/scheme48.def
	$(INSTALL_DATA) $(srcdir)/c/scheme48.h $(DESTDIR)$(incdir)
	$(INSTALL_DATA) c/scheme48arch.h $(DESTDIR)$(incdir)
	$(INSTALL_DATA) $(srcdir)/c/scheme48write-barrier.h $(DESTDIR)$(incdir)
	$(INSTALL_DATA) $(srcdir)/c/scheme48.exp $(DESTDIR)$(incdir)
	$(INSTALL_DATA) $(srcdir)/c/scheme48.def $(DESTDIR)$(incdir)
	$(INSTALL_DATA) $(srcdir)/c/scheme48-external.exp $(DESTDIR)$(incdir)

inst-misc:
	for stub in env big sort opt misc link posix cml srfi; do	\
		for f in $(srcdir)/scheme/$$stub/*.scm; do		\
			$(INSTALL_DATA) $$f $(DESTDIR)$(SHARE)/$$stub || exit 1;	\
		done;							\
	done
	for f in $(srcdir)/scheme/rts/*num.scm 				\
		 $(srcdir)/scheme/rts/jar-defrecord.scm; do		\
		$(INSTALL_DATA) $$f $(DESTDIR)$(SHARE)/rts || exit 1;	\
	done

inst-script:
	script=$(DESTDIR)$(bindir)/$(RUNNABLE) &&			\
	LIB=$(LIB) &&							\
	echo "#!/bin/sh"	>$$script &&				\
	echo			>>$$script &&				\
	echo "lib=$$LIB"	>>$$script &&				\
	echo 'exec $$lib/$(VM) -i $$lib/$(IMAGE) "$$@"'			\
				>>$$script &&				\
	chmod +x $$script

inst-script-interpreters:
	for dialect in r5rs srfi-7; do					\
		$(INSTALL_PROGRAM) script-interpreter			\
			$(DESTDIR)$(bindir)/scheme-$$dialect;		\
	done

inst-config-script:
	config_script=$(bindir)/$(CONFIG_SCRIPT) &&			\
	sed -e 's,@incdir\@,$(incdir),g'				\
	    -e 's,@libdir\@,$(libdir),g'				\
	    -e 's,@CC\@,$(CC),g'					\
	    -e 's,@LD\@,$(LD),g'					\
	    -e 's,@DYNAMIC_EXTERNALS_CFLAGS\@,$(DYNAMIC_EXTERNALS_CFLAGS),g' \
	    -e 's,@DYNAMIC_EXTERNALS_LDFLAGS\@,$(DYNAMIC_EXTERNALS_LDFLAGS),g' \
	< $(srcdir)/build/$(CONFIG_SCRIPT).in > $$config_script &&	\
	chmod +x $$config_script

inst-externals:
	$(INSTALL_PROGRAM) $(POSIX_EXTERNAL) $(SRFI_27_EXTERNAL) $(DESTDIR)$(LIB)/

# Script to run scheme48 in this directory.
go:
	echo '#!/bin/sh'	>$@ &&					\
	echo			>>$@ &&					\
	echo "lib=\"`pwd`\""	>>$@ &&					\
	echo 'exec "$$lib"/$(VM) -i "$$lib"/$(IMAGE) "$$@"'		\
				>>$@ &&					\
	chmod +x $@

# Exports file for AIX and Cygwin

CAT_EXPORTS = cat $(srcdir)/c/scheme48.h $(srcdir)/c/fd-io.h $(srcdir)/c/event.h | egrep '^S48_EXTERN' | sed 's,^S48_EXTERN.*\(s48_[a-zA-Z0-9_]*\)(.*$$,\1,' | sed 's,^S48_EXTERN.*\(ps_[a-zA-Z0-9_]*\)(.*$$,\1,' | sed 's,^S48_EXTERN.*\(s48_[a-zA-Z0-9_]*\).*$$,\1,'

c/scheme48.exp: c/scheme48.h c/fd-io.h c/event.h
	echo '#!..' > $(srcdir)/c/scheme48.exp &&			\
	$(CAT_EXPORTS) >> $(srcdir)/c/scheme48.exp

c/scheme48.def: c/scheme48.h c/fd-io.h c/event.h
	echo 'EXPORTS' > $(srcdir)/c/scheme48.def &&			\
	$(CAT_EXPORTS) >> $(srcdir)/c/scheme48.def

dirs:
	for dir in $(libdir) $(bindir) $(incdir) $(datadir); do			 \
		$(srcdir)/mkinstalldirs $(DESTDIR)$$dir || exit 1;	 \
	done
	$(srcdir)/mkinstalldirs $(DESTDIR)$(LIB)
	for dir in rts env big sort opt misc link posix cml srfi; do	  \
		$(srcdir)/mkinstalldirs $(DESTDIR)$(SHARE)/$$dir || exit 1; \
	done

configure: configure.in
	autoheader && autoconf

clean:
	-rm -f $(VM) script-interpreter					\
		*.o c/unix/*.o c/posix/*.o c/*.o c/fake/*.o c/bibop/*.o	\
		$(POSIX_EXTERNAL) $(SRFI_27_EXTERNAL)			\
		c/*.a c/glue_gnu.s c/glue_darwin.s c/scheme48.exp 	\
		TAGS $(IMAGE)						\
		$(srcdir)/build/*.tmp $(srcdir)/$(MANPAGE) 		\
		$(srcdir)/scheme/debug/*.image 				\
		$(srcdir)/scheme/debug/*.debug 				\
		$(srcdir)/scheme/vm/scheme48vm-??.c 			\
		$(srcdir)/scheme/vm/scheme48*heap-??.c			\
		go $(distname)

distclean: clean
	rm -f Makefile config.log config.status config-cache \
		c/sysdep.h c/scheme48arch.h

check:	$(VM) $(IMAGE) $(POSIX_EXTERNAL) $(SRFI_27_EXTERNAL) scheme/debug/check.scm
	(								\
		echo ',batch';						\
                echo ',exec ,load =scheme48/debug/check.scm';           \
		echo ',config ,load =scheme48/test-packages.scm';	\
		echo ',open big-test';					\
		echo ',open sockets-test';				\
		echo ',config ,load =scheme48/misc/packages.scm';	\
		echo ',config ,load =scheme48/misc/test-packages.scm';	\
		echo ',open misc-test';					\
		echo ',config ,load =scheme48/cml/test-packages.scm';	\
		echo ',open cml-test';					\
		echo ',config ,load =scheme48/srfi/test-packages.scm';	\
		echo ',open srfi-test';					\
		echo ',config ,load =scheme48/posix/test-packages.scm';	\
		echo ',open posix-test';				\
		echo ',open test-suites';				\
		echo '(define-test-suite all-tests (compiler-tests big-tests tcp-sockets-tests misc-tests cml-tests srfi-tests posix-tests))'; \
		echo '(run-test-suite all-tests)'			\
	) | ./$(VM) -i $(IMAGE) -h 8000000

# --------------------
# Rules from here on down are not essential for the basic installation
# procedure.

all: vm image script-interpreter go
vm: $(VM)
image: $(INITIAL)-$(BIT_SUFFIX)
	$(MAKE) $(IMAGE)
test: check

tags:
	find . -name "*.scm" -o -name "*.c" -o -name "*.h" | etags -

# --------------------
# Distribution...

# DISTFILES should include all sources.
DISTFILES = README COPYING.rtf COPYING INSTALL WINDOWS.txt		\
		configure configure.in config.sub config.guess 		\
		acconfig.h Makefile.in 					\
		scheme48.sln scheme48.vcproj				\
		mkinstalldirs install-sh				\
		doc/*.ps doc/*.pdf doc/*.txt 				\
		doc/html/*.html doc/html/*.css doc/html/*.gif 		\
		doc/src/*.tex doc/src/*.sty doc/src/*.scm		\
		doc/scheme48.man					\
		emacs/README 						\
		build/*.scm build/*-version-number build/*.exec		\
		build/*.lisp build/build-usual-image build/filenames.make \
		build/initial.debug 					\
		build/initial.image-32 build/initial.image-64		\
		build/*.bat build/*.sh build/*.rc			\
		build/dummy build/*.wxs build/*.wxi			\
		build/*.bmp build/*.ico					\
		build/scheme48-config.in				\
		c/*.[ch] c/*/*.[ch] c/*/*.ci c/*.exp			\
		c/scheme48.h.in						\
		c/scheme48write-barrier.h c/scheme48arch.h.in		\
		emacs/*.el gdbinit					\
		scheme/*.scm scheme/*/*.scm scheme/vm/*/*.scm		\
		ps-compiler/minor-version-number			\
		ps-compiler/doc/node.txt ps-compiler/doc/todo.txt	\
		ps-compiler/*.scm ps-compiler/*/*.scm			\
		ps-compiler/prescheme/primop/*.scm			\
		ps-compiler/prescheme/test/*.scm			\
		c/sysdep.h.in

distname = scheme48-$(VERSION)

dist: build/initial.image-32 build/initial.image-64 c/scheme48.h
	distname=$(distname) &&						\
	distfile=$(distdir)/$$distname.tgz &&				\
	if [ -d $(distdir) ] &&						\
	   [ -w $$distfile -o -w $(distdir) ]; then			\
		cd $(srcdir) &&						\
		rm -f $$distname &&					\
		ln -s . $$distname &&					\
		files='' &&						\
		for i in $(DISTFILES); do				\
			if [ "$$i" != "c/sysdep.h" ]; then		\
				files="$$files $$distname/$$i";		\
			fi						\
		done &&							\
		tar -cf - $$files |					\
			gzip --best >$$distfile &&			\
		rm $$distname;						\
	else								\
		echo "Can't write $$distfile" >&2;			\
		exit 1;							\
	fi

# Increment the minor version number
inc:
	f=build/minor-version-number &&					\
	expr `cat $$f` + 1 >$$f.tmp &&					\
	mv $$f.tmp $$f &&						\
	cp $$f doc/src/version-number.tex &&				\
	echo '(define version-info "1.'`cat $$f`'")'			\
		>scheme/env/version-info.scm


# --------------------
# Generate build/filenames.make from *packages.scm
#
# This hack traces the module dependencies described in the
# various configuration files and converts them into dependency lists
# that "make" can use for its purposes.
#
# Since the distribution comes with a filenames.make, this rule
# shouldn't be invoked for simple installations.  But it will be used
# if you change any of the *-packages.scm files.
#
# You can actually run the forms in filenames.scm in any Scheme
# implementation that has syntax-rules and explicit-renaming low-level
# macros (e.g., most versions of Scheme 48 and Pseudoscheme).
# If there are errors running this script, and you need to debug,
# don't use the initial.image, use something that has a reasonable
# environment.
#
# If this fails and you don't feel like debugging or fixing the problem,
# try "touch filenames.make" and hope for the best.

PACKAGES=scheme/packages.scm scheme/rts-packages.scm scheme/alt-packages.scm \
	 scheme/comp-packages.scm scheme/initial-packages.scm \
	 scheme/link-packages.scm scheme/env-packages.scm \
	 scheme/sort/packages.scm scheme/more-packages.scm \
	 scheme/cml/packages.scm \
	 build/filenames.scm

$(srcdir)/build/filenames.make: $(PACKAGES)
# uncomment this if you activate the line below:
#	$(MAKE) $(VM) PACKAGES=
	cd $(srcdir) && \
	$(RUNNABLE) -a batch <build/filenames.scm
# or:	./$(VM) -i $(srcdir)/$(INITIAL)-$(BIT_SUFFIX) -a batch <build/filenames.scm


# --------------------
# Static linker
#
# The linker is capable of rebuilding an image from sources, even
# across an incompatible change in VM data representations.

# Bootstrap from Lucid Common Lisp: (last tested with
# Pseudoscheme 2.9 and Scheme 48 version 0.19)

PSEUDODIR = ../pseudo

link/linker-in-lucid: build/lucid-script.lisp $(linker-files) \
	    scheme/alt/pseudoscheme-features.scm \
	    scheme/alt/pseudoscheme-record.scm
	(echo \(defvar pseudoscheme-directory \"$(PSEUDODIR)/\"\); \
	 cat build/lucid-script.lisp; \
	 echo \(dump-linker\) \(lcl:quit\)) \
	| lisp

# --------------------
# Initial image
#
# The initial.image is built by the static linker.  The image contains
# Scheme, the byte-code compiler, and a minimal command processor, but
# no debugging environment to speak of.

$(INITIAL)-32: $(CONFIG_FILES) build/initial.scm scheme/rts-packages-32.scm $(initial-files) $(linker-files)
	cd $(srcdir) &&							\
	($(START_LINKER-32);						\
		echo '(load-configuration "scheme/platform-interfaces.scm")'; \
		echo '(load-configuration "scheme/rts-packages-32.scm")'; \
		echo '(load-configuration "scheme/interfaces.scm")';	\
		echo '(load-configuration "scheme/vm/shared-interfaces.scm")'; \
		echo '(load-configuration "scheme/packages.scm")';	\
		echo '(flatload initial-structures)';			\
		echo '(load "build/initial.scm")';			\
		echo '(link-initial-system)'				\
	) | $(LINKER_RUNNABLE)
	mv $(INITIAL) $@

$(INITIAL)-64: $(CONFIG_FILES) build/initial.scm scheme/rts-packages-64.scm $(initial-files) $(linker-files)
	cd $(srcdir) &&							\
	($(START_LINKER-64);						\
		echo '(load-configuration "scheme/platform-interfaces.scm")'; \
		echo '(load-configuration "scheme/rts-packages-64.scm")'; \
		echo '(load-configuration "scheme/interfaces.scm")';	\
		echo '(load-configuration "scheme/vm/shared-interfaces.scm")'; \
		echo '(load-configuration "scheme/packages.scm")';	\
		echo '(flatload initial-structures)';			\
		echo '(load "build/initial.scm")';			\
		echo '(link-initial-system)'				\
	) | $(LINKER_RUNNABLE)
	mv $(INITIAL) $@

# --------------------
# Various small images for debugging low-level changes

LOAD_DEBUG = \
	 $(START_LINKER-32); \
	 echo \(load-configuration \"scheme/interfaces.scm\"\); \
	 echo \(load-configuration \"scheme/packages.scm\"\); \
	 echo \(load-configuration \"scheme/vm/shared-interfaces.scm\"\); \
	 echo \(flatload debug-structures\)

scheme/debug/tiny.image: scheme/debug/tiny-packages.scm \
		  scheme/debug/tiny.scm
	cd $(srcdir) && \
	($(START_LINKER-32); \
	 echo \(load-configuration \"scheme/debug/tiny-packages.scm\"\); \
	 echo \(link-simple-system \'\(scheme/debug tiny\) \'start tiny-system\)) \
	| $(LINKER_RUNNABLE)

scheme/debug/low-test.image: scheme/debug/low-test-packages.scm \
		  scheme/debug/low-test.scm
	cd $(srcdir) && \
	($(START_LINKER-32); \
	 echo \(load-configuration \"scheme/debug/low-test-packages.scm\"\); \
	 echo \(link-simple-system \'\(scheme/debug low-test\) \'start low-test-system\)) \
	| $(LINKER_RUNNABLE)

scheme/debug/bignum-test.image: scheme/debug/low-test-packages.scm \
		  scheme/debug/bignum-test.scm
	cd $(srcdir) && \
	($(START_LINKER-32); \
	 echo \(load-configuration \"scheme/debug/low-test-packages.scm\"\); \
	 echo \(link-simple-system \'\(scheme/debug bignum-test\) \'start bignum-test-system\)) \
	| $(LINKER_RUNNABLE)

scheme/debug/little.image: $(CONFIG_FILES) scheme/debug-packages.scm
	cd $(srcdir) &&					\
	($(LOAD_DEBUG); echo \(link-little-system\))	\
	| $(LINKER_RUNNABLE)

scheme/debug/mini.image: $(CONFIG_FILES)
	cd $(srcdir) &&					\
	($(LOAD_DEBUG); echo \(link-mini-system\))	\
	| $(LINKER_RUNNABLE)

scheme/debug/medium.image: $(CONFIG_FILES)
	cd $(srcdir) &&						\
	($(LOAD_DEBUG); echo \(flatload compiler-structures\);	\
	 echo \(link-medium-system\)) \
	| $(LINKER_RUNNABLE)

# The following have not been updated for the new directory organization

c/smain.o: c/main.c
	$(CC) -c $(CPPFLAGS) $(DEFS) $(CFLAGS) -DSTATIC_AREAS -o $@ $(srcdir)/c/main.c

mini: mini-heap.o smain.o
	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ c/smain.o mini-heap.o $(OBJS) $(LIBS)

mini-heap.o: mini-heap.c
	$(CC) -c $(CPPFLAGS) $(DEFS) $(CFLAGS) -o $@ $(srcdir)/mini-heap.c

mini-heap.c: scheme/debug/mini1.image
	(echo ,exec ,load misc/load-static.scm; \
	 echo \(do-it 150000 \"$(srcdir)/scheme/debug/mini1.image\" \"$@\"\)) \
	| $(RUNNABLE) -h 3000000 -a batch

scheme/debug/mini1.image: $(VM) scheme/debug/mini.image
	echo "(write-image \"scheme/debug/mini1.image\" \
			   (usual-resumer (lambda (args) \
					    (command-processor #f args))) \
			   \"foo\")" \
	| ./$(VM) -i scheme/debug/mini.image -a batch


# --------------------
# Generate unicode-info.scm, srfi-14-base-char-set.scm, and unicode-normalization-info
# from UnicodeData.txt
# Get fresh UnicodeData.txt from http://www.unicode.org.

scheme/rts/syntax-info.scm: build/UnicodeData.txt			\
	build/PropList.txt						\
	build/SpecialCasing.txt						\
	build/CaseFolding.txt						\
	build/CompositionExclusions.txt					\
	scheme/link/unicode-data.scm 					\
	scheme/env/unicode-category.scm					\
	scheme/big/compact-table.scm
	cd $(srcdir) &&							\
		(echo ',batch';							\
		echo ',config ,load scheme/link/unicode-data-packages.scm';	\
		echo ",in unicode-data (create-unicode-tables \"build/UnicodeData.txt\" \"build/PropList.txt\" \"build/SpecialCasing.txt\" \"build/CaseFolding.txt\" \"build/CompositionExclusions.txt\" \"scheme/env/unicode-info.scm\" \"scheme/rts/syntax-info.scm\" \"scheme/big/unicode-normalization-info.scm\" \"scheme/srfi/srfi-14-base-char-sets.scm\")"		\
	) | $(RUNNABLE) -h 6000000

# --------------------
# Generate scheme48.h from VM sources

c/scheme48.h: c/scheme48.h.in scheme/vm/interp/arch.scm 		\
		scheme/vm/data/data.scm scheme/link/generate-c-header.scm
	cd $(srcdir) &&							\
	(echo ',bench';							\
		echo ',batch';						\
		echo ',load-package big-scheme';			\
		echo ',open big-scheme';				\
		echo ',load scheme/link/generate-c-header.scm';		\
		echo "(make-c-header-file \"$@\"			\
			  \"c/scheme48.h.in\"				\
			  \"scheme/vm/interp/arch.scm\"			\
			  \"scheme/vm/data/data.scm\"			\
			  \"scheme/rts/record.scm\")"			\
	) | $(RUNNABLE)

# An old version of the above for legacy code.

c/old-scheme48.h: scheme/vm/interp/arch.scm scheme/vm/data/data.scm \
	    scheme/link/generate-old-c-header.scm
	(echo ',bench';							\
		echo ',batch';						\
		echo ',load-package big-scheme';			\
		echo ',open big-scheme';				\
		echo ',load scheme/link/generate-old-c-header.scm';	\
		echo "(make-c-header-file \"$@\"			\
			  \"$(srcdir)/scheme/vm/interp/arch.scm\"	\
			  \"$(srcdir)/scheme/vm/data/data.scm\")"   	\
	) | $(RUNNABLE)

# Generate VM and GC from VM sources.
# Never called automatically.  Do not use unless you are sure you
# know what you are doing.
# Afterwards, you should probably make c/scheme48.h.

i-know-what-i-am-doing: \
	compile-vm-32 compile-twospace-gc-32 compile-bibop-gc-32 \
	compile-vm-64 compile-twospace-gc-64 compile-bibop-gc-64

# compiles VM without GC

compile-vm-32:
	cd $(srcdir)/ps-compiler &&					     \
	(echo ',batch';							     \
		echo ',config ,load ../scheme/platform-interfaces.scm';	     \
		echo ',config ,load ../scheme/rts-packages-32.scm';          \
		echo ',config ,load ../scheme/prescheme/interface.scm';	     \
		echo ',config ,load ../scheme/prescheme/package-defs.scm';   \
		echo ',exec ,load load-ps-compiler.scm';		     \
		echo ',exec ,load compile-vm-no-gc-32.scm';		     \
		echo ',exit'						     \
	) | $(RUNNABLE) -h 12000000 &&					     \
		mv ../scheme/vm/scheme48vm-32.c ../c

compile-vm-64:
	cd $(srcdir)/ps-compiler &&					     \
	(echo ',batch';							     \
		echo ',config ,load ../scheme/platform-interfaces.scm';	     \
		echo ',config ,load ../scheme/rts-packages-64.scm';          \
		echo ',config ,load ../scheme/prescheme/interface.scm';	     \
		echo ',config ,load ../scheme/prescheme/package-defs.scm';   \
		echo ',exec ,load load-ps-compiler.scm';		     \
		echo ',exec ,load compile-vm-no-gc-64.scm';		     \
		echo ',exit'						     \
	) | $(RUNNABLE) -h 12000000 &&					     \
		mv ../scheme/vm/scheme48vm-64.c ../c

# compiles the two-space copier GC

compile-twospace-gc-32:
	cd $(srcdir)/ps-compiler &&					     \
	(echo ',batch';							     \
		echo ',config ,load ../scheme/platform-interfaces.scm';	     \
		echo ',config ,load ../scheme/rts-packages-32.scm';          \
		echo ',config ,load ../scheme/prescheme/interface.scm';	     \
		echo ',config ,load ../scheme/prescheme/package-defs.scm';   \
		echo ',exec ,load load-ps-compiler.scm';		     \
		echo ',exec ,load compile-twospace-gc-32.scm';		     \
		echo ',exit'						     \
	) | $(RUNNABLE) -h 8000000 &&					     \
		mv ../scheme/vm/scheme48-twospace-heap-32.c ../c

compile-twospace-gc-64:
	cd $(srcdir)/ps-compiler &&					     \
	(echo ',batch';							     \
		echo ',config ,load ../scheme/platform-interfaces.scm';	     \
		echo ',config ,load ../scheme/rts-packages-64.scm';          \
		echo ',config ,load ../scheme/prescheme/interface.scm';	     \
		echo ',config ,load ../scheme/prescheme/package-defs.scm';   \
		echo ',exec ,load load-ps-compiler.scm';		     \
		echo ',exec ,load compile-twospace-gc-64.scm';		     \
		echo ',exit'						     \
	) | $(RUNNABLE) -h 8000000 &&					     \
		mv ../scheme/vm/scheme48-twospace-heap-64.c ../c

# compiles the BIBOP GC

compile-bibop-gc-32:
	cd $(srcdir)/ps-compiler &&					     \
	(echo ',batch';							     \
		echo ',config ,load ../scheme/platform-interfaces.scm';	     \
		echo ',config ,load ../scheme/rts-packages-32.scm';          \
		echo ',config ,load ../scheme/prescheme/interface.scm';	     \
		echo ',config ,load ../scheme/prescheme/package-defs.scm';   \
		echo ',exec ,load load-ps-compiler.scm';		     \
		echo ',exec ,load compile-bibop-gc-32.scm';		     \
		echo ',exit'						     \
	) | $(RUNNABLE) -h 8000000 &&					     \
		mv ../scheme/vm/scheme48-bibop-heap-32.c ../c

compile-bibop-gc-64:
	cd $(srcdir)/ps-compiler &&					     \
	(echo ',batch';							     \
		echo ',config ,load ../scheme/platform-interfaces.scm';	     \
		echo ',config ,load ../scheme/rts-packages-64.scm';          \
		echo ',config ,load ../scheme/prescheme/interface.scm';	     \
		echo ',config ,load ../scheme/prescheme/package-defs.scm';   \
		echo ',exec ,load load-ps-compiler.scm';		     \
		echo ',exec ,load compile-bibop-gc-64.scm';		     \
		echo ',exit'						     \
	) | $(RUNNABLE) -h 8000000 &&					     \
		mv ../scheme/vm/scheme48-bibop-heap-64.c ../c
